﻿using System;
using System.IO;
using System.Windows.Forms;
using LotroMusicManager.Properties;

namespace LotroMusicManager
{
    public partial class FormExportMacros : Form
    {
        public FormExportMacros()
        {   //====================================================================
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {   //====================================================================
            // Populate the macro list
            foreach (Macro m in Settings.Default.Macros.Items) 
            {
                ListViewItem lvi = new ListViewItem(m.Name);
                lvi.Tag = m;
                if (m.ImagePath != null && m.ImagePath != String.Empty) 
                {
                    try
                    {
                        imglst.Images.Add(m.ID, new System.Drawing.Bitmap(m.ImagePath));
                        lvi.ImageKey = m.ID;
                    } catch {;} // A throw just means we should ignore that image
                }
                lstMacros.Items.Add(lvi);
            }
            lstMacros.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);

            // Not ready to save things
            btnOK.Enabled = false;

            return;
        }

        private void OnExportTypeChanged(object sender, EventArgs e)
        {   //====================================================================
            txtExportFileName.Enabled = rdoExportToFile.Checked;
            btnBrowseForFile.Enabled  = rdoExportToFile.Checked;
            return;
        }

        private void OnBrowseForFile(object sender, EventArgs e)
        {   //====================================================================
            if (dlgSaveFile.ShowDialog() == DialogResult.OK)
            {
                txtExportFileName.Text = dlgSaveFile.FileName;
            }
            return;
        }

        private void OnItemChecked(object sender, ItemCheckedEventArgs e)
        {   //====================================================================
            // Possibly not the simplest line to read, but conceptually easy
            btnOK.Enabled = (lstMacros.CheckedItems.Count > 0) && (rdoExportToClipboard.Checked || txtExportFileName.Text != String.Empty);
            return;
        }

        private void OnExport(object sender, EventArgs e)
        {   //====================================================================
            MacroList ml = new MacroList();
            foreach (ListViewItem lvi in lstMacros.CheckedItems)
            {
                ml.Add((Macro)lvi.Tag);
            }
            
            if (rdoExportToClipboard.Checked)
            {
                Clipboard.SetText(ml.GetXML());
            }

            if (rdoExportToFile.Checked)
            {
                try
                {
                    FileInfo fi = new FileInfo(txtExportFileName.Text);
                    if (fi.Exists) fi.Delete();
                    StreamWriter sw = fi.CreateText();
                    sw.Write(ml.GetXML());
                    sw.Close();
                    sw.Dispose();                    
                }
                catch {;}
                
            }

            return;
        }

        private void OnFileChanged(object sender, EventArgs e)
        {   //====================================================================
            btnOK.Enabled = (lstMacros.CheckedItems.Count > 0) && (rdoExportToClipboard.Checked || txtExportFileName.Text != String.Empty);
            return;
        }
    }
}
